import TRTC from "trtc-js-sdk";
import store from "@/store";
// import TimeUtil from "./TimeUtil";
import {uploadBase64} from "@/api/filecenter/info.js";
import {getStore} from '@/util/store'

let client;
let localStream;
let remoteStream;
let callerStream;
let imageInfoList = [];

export default {
  loginTRTC(localStreamElementId, remoteStreamElementId) {
    let _incomingMessage = store.state.MiniAccident.incomingMessage;
    let _userInfo = getStore({name: 'userInfo'});
    client = TRTC.createClient({
      mode: "rtc",
      sdkAppId: parseInt(_incomingMessage.sdkAppID),
      userId: _userInfo.userId,
      userSig: _incomingMessage.userSig
    });
    client.join({
      roomId: parseInt(_incomingMessage.roomId),
      privateMapKey: _incomingMessage.privateMapKey
    }).then(() => {
      localStream = TRTC.createStream({
        userId: _userInfo.userId,
        audio: true,
        video: true
      });
      localStream.initialize().then(() => {
        /*
        let _local = document.getElementById("local");
        _local.srcObject = info.stream;
        _local.addEventListener("timeupdate",function(){
            let timeDisplay = Math.floor(_local.currentTime);
            that.videoCurTime = TimeUtil.secondTohour(timeDisplay)
        },false);
        */
        localStream.play(localStreamElementId);
        client.publish(localStream);
      });
    });
    client.on("stream-added", event => {
      // console.log("stream-added---->", event);
      client.subscribe(event.stream);
    });
    client.on("stream-subscribed", event => {
      // console.log("stream-subscribed---->", event);
      let _stream = event.stream;
      remoteStream = _stream;
      remoteStream.play(remoteStreamElementId).then(function (e) {
        // console.log("_incomingMessage.callerId---->", _incomingMessage.callerId);
        // console.log("remoteStream.getUserId()---->", _stream.getUserId());
        if (_incomingMessage.callerId == _stream.getUserId()) {
          // console.log("1-----", _stream);
          callerStream = _stream;
        }
      });
    });
    client.on('stream-updated', event => {
      // console.log("stream-updated---->", event);
    });
    client.on('stream-removed', event => {
      // console.log("stream-removed---->", event);
      let id = event.stream.getId();
      if (id) {
        let _play = "#player_" + id;
        let _melement = document.querySelector(_play);
        _melement.style = "display: none";
      }
    });
  },
  snapshot() {
    let frame = callerStream.getVideoFrame();
    if (frame) {
      uploadBase64({
        imageBase: frame
      }).then(function (res) {
        imageInfoList.push({
          imgPath: res.data.data.path
        });
        store.commit("setImageInfoList", imageInfoList);
      })
    }
  },
  resetImageInfoList() {
    imageInfoList = [];
  },
  logoutTRTC() {
    localStream.stop();
    localStream.close();
    client.unpublish(localStream);
    if (remoteStream) {
      remoteStream.stop();
      remoteStream.close();
      client.unsubscribe(remoteStream);
    }
    client.leave();
  }
};
